package com.link.base.base.home.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.home.dao.mybatis.mapper.HomeMapper;
import com.link.base.base.home.model.HomeReport;
import com.link.core.util.RedisConstants;
import com.link.core.util.RedisDataReportUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: unknown
 * created: unknown
 * comments: 查询本周记录
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private HomeMapper homeMapper;


    @Override
    public String queryCLueCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            //缓存时间大于0且小于一天，则执行缓存查询
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryCLueCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryCLueCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryCLueCurentWeekCount(homeReport);
        }
    }

    @Override
    public String queryOptyCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryOptyCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryOptyCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryOptyCurentWeekCount(homeReport);
        }
//      return homeMapper.queryOptyCurentWeekCount(user);
    }

    @Override
    public String queryAccountCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryAccountCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryAccountCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryAccountCurentWeekCount(homeReport);
        }
//      return homeMapper.queryAccountCurentWeekCount(user);
    }

    @Override
    public String queryOrderCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryOrderCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryOrderCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryOrderCurentWeekCount(homeReport);
        }
//      return homeMapper.queryOrderCurentWeekCount(user);
    }

    @Override
    public String queryActivityCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryActivityCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryActivityCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryActivityCurentWeekCount(homeReport);
        }
//      return homeMapper.queryActivityCurentWeekCount(user);
    }

    @Override
    public String queryWorkPlanCurentWeekCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        int countSeconds = 86400;
        int seconds = RedisDataReportUtil
                .getCfgCorpReportTimeProperty(RedisConstants.SYS_PROPERTY_CORP_KEY + user.getLoginCorpId(), RedisDataReportUtil.REPORT_TIME_FIELD);
        if (seconds > 0 && seconds < countSeconds) {
            String key = RedisDataReportUtil
                    .generateRedisKey(RedisDataReportUtil.CORP_TYPE, String.valueOf(user.getCorpid()), "queryWorkPlanCurentWeekCount");
            String value = RedisDataReportUtil.getReportStringDataFromRedis(key);
            if (!StringUtils.isBlank(value)) {
                return value;
            } else {
                String result = homeMapper.queryWorkPlanCurentWeekCount(homeReport);
                RedisDataReportUtil.addReportStringDataToRedis(key, result, seconds);
                return result;
            }
        } else {
            return homeMapper.queryWorkPlanCurentWeekCount(homeReport);
        }
//      return homeMapper.queryWorkPlanCurentWeekCount(user);
    }


    /**
     * 查询所有未阅读消息数量
     *
     * @author weixuan
     * @return
     * @throws Exception
     */
    @Override
    public String queryMyNewsCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setCreatedBy(user.getId());
        String result = homeMapper.queryMyNewsCount(homeReport);
        return result;
    }

    /**
     * 查询所有未阅读公告数量
     *
     * @author weixuan
     * @return
     * @throws Exception
     */
    @Override
    public String queryMyAnnounceCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setCreatedBy(user.getId());

        String result = homeMapper.queryMyAnnounceCount(homeReport);

        return result;
    }

    /**
     * 查询所有未完成工作计划数量
     *
     * @author weixuan
     * @return
     * @throws Exception
     */
    @Override
    public String queryMyWorkPlanCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setCreatedBy(user.getId());
        String result = homeMapper.queryMyWorkPlanCount(homeReport);
        return result;
    }

    /**
     * 查询所有发起未完成
     *
     * @author weixuan
     * @return
     * @throws Exception
     */
    @Override
    public String queryMyApplyCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setCreatedBy(user.getId());
        String result = homeMapper.queryMyApplyCount(homeReport);
        return result;
    }

    /**
     * 查询所有待办审批数量
     *
     * @author weixuan
     * @return
     * @throws Exception
     */
    @Override
    public String queryMyFlowCount() throws Exception {
        CoreUser user = UserUtil.getUser();
        HomeReport homeReport = new HomeReport();
        homeReport.setLoginCorpId(user.getLoginCorpId());
        homeReport.setCreatedBy(user.getId());
        String result = homeMapper.queryMyFlowCount(homeReport);
        return result;
    }


}
